package com.jhon.yinji.lichuanvip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jhon.yinji.lichuanvip.fragment.LoginFragment;
import com.jhon.yinji.lichuanvip.fragment.MainFragment;
import com.jhon.yinji.lichuanvip.fragment.RegisterFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * Created by yinji on 2016/7/13.
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @ViewInject(R.id.am_rb_index)
    private RadioButton am_rb_index;

    @ViewInject(R.id.am_rb_persioninfo)
    private RadioButton am_rb_persioninfo;

    @ViewInject(R.id.am_rg_nav)
    private RadioGroup am_rg_nav;

    private static FragmentManager fragmentManager;
    private static Fragment nowFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        am_rg_nav.setOnCheckedChangeListener(this);
        fragmentManager = getSupportFragmentManager();
        nowFragment = new MainFragment();
        fragmentManager.beginTransaction().add(R.id.am_fl, nowFragment, "MainFragment").commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (am_rb_index.isChecked()) {
            am_rb_index.setTextColor(getResources().getColor(R.color.nav_radio_check));
            am_rb_persioninfo.setTextColor(getResources().getColor(R.color.generic_summary_new_color));
            switchFtagmet("MainFragment");
        }
        if (am_rb_persioninfo.isChecked()) {
            am_rb_persioninfo.setTextColor(getResources().getColor(R.color.nav_radio_check));
            am_rb_index.setTextColor(getResources().getColor(R.color.generic_summary_new_color));
            switchFtagmet("LoginFragment");
        }
    }

    public static void switchFtagmet(String string) {
        Fragment fragmentByTag = fragmentManager.findFragmentByTag(string);
        if (fragmentByTag != null) {
            fragmentManager.beginTransaction().hide(nowFragment).show(fragmentByTag).commit();
            nowFragment = fragmentByTag;
        } else {
            Fragment fragment = null;
            if (string.equals("MainFragment"))
                fragment = new MainFragment();
            else if (string.equals("LoginFragment"))
                fragment = new LoginFragment();
            else if (string.equals("RegisterFragment"))
                fragment = new RegisterFragment();
            fragmentManager.beginTransaction().hide(nowFragment).add(R.id.am_fl, fragment, string).commit();
            nowFragment = fragment;
        }
    }
}
